﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess.DataInf;
using HTTT.Utilities;

namespace HTTT.DataAccess
{
    public class HT_STCountDAO : IHT_STCountDAO
    {
        /// <summary>
        /// Gets all HT_STCount
        /// </summary>
        /// <returns></returns>
        /// 

        private static AppEntities dataContext;

        private IRepository<HT_STCount> repository;

        public HT_STCountDAO()
        {
            dataContext = new AppEntities();
            repository = new RepositoryDAO<HT_STCount>(dataContext);
        }


        public IEnumerable<HT_STCount> GetAllHT_STCount()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.CountDate);

        }

        public HT_STCount GetHT_STCountByID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();

        }

        public bool InsertHT_STCount(HT_STCount inHT_STCount)
        {
            if (inHT_STCount == null) return false;
            repository.Add(inHT_STCount);
            return repository.SaveChanges() > 0;
        }

        public bool UpdateHT_STCount(HT_STCount inHT_STCount)
        {
            if (inHT_STCount == null) return false;
            HT_STCount upHT_STCount = repository.First(obj => obj.ID == inHT_STCount.ID);

            if (upHT_STCount == null) return false;
            upHT_STCount.CountDate = inHT_STCount.CountDate;
            upHT_STCount.Number = inHT_STCount.Number;
            upHT_STCount.Username = inHT_STCount.Username;
            return repository.SaveChanges() > 0;
        }


        public ReturnValue<bool> DeleteHT_STCount(Guid id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                HT_STCount objDel = repository.First(obj => obj.ID == id);
                ret = DelHT_STCount(objDel);
            }
            catch (Exception)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;

        }
        public ReturnValue<bool> DelHT_STCount(HT_STCount inHT_STCount)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inHT_STCount == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inHT_STCount);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }

        public IEnumerable<HT_STCount> SearchHT_STCount(string keyword)
        {
            try
            {
                var r = from m in dataContext.HT_STCount
                        where(m.Username.Contains(keyword))
                        select m;
                return r;

            }
            catch (Exception)
            {
                return null;
            }
        }

        public ReturnValue<bool> InsertLoginCount(List<HT_UserLoginCount> ListUserLoginCount)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                int getcount = 0;
                DateTime Now = DateTime.Now;
                foreach (HT_UserLoginCount count in ListUserLoginCount)
                {
                    HT_STCount NewItem = new HT_STCount();
                    NewItem.ID = Guid.NewGuid();
                    NewItem.Username = count.UserName;
                    NewItem.Number = count.LoginCount;
                    NewItem.CountDate = Now;
                    repository.Add(NewItem);
                    getcount ++;
                }
                ret.RetValue = repository.SaveChanges() > 0;
                ret.Msg = "Lấy dữ liệu thành công " + getcount + " bản ghi !";
            }
            catch (Exception)
            {
                ret.RetValue = false;
                ret.Msg = "Lỗi insert data base";
            }
            return ret;
        }
    }
}
